<script src="../utils/printLinkNode.js"></script>
<script type="text/javascript">
	
function RandomListNode(x){
    this.label = x;
    this.next = null;
    this.random = null;
}
let a = new RandomListNode(1)
let b = new RandomListNode(2)
let c = new RandomListNode(3)
let d = new RandomListNode(4)
a.next = b;
a.random = d;
b.next = c;
b.random = c;
c.next = d;
c.random = b;
d.random = a;

function Clone(pHead)
{
    // 创建新结点，结构如下, 其中()表示新创建的一个结点
    // 1->(1)->2->(2)->3->(3)->4->(4)
    if(pHead === null) return null;
    let pre = pHead;
    while(pre!==null){
        let newNode = new RandomListNode(pre.label);
        newNode.next = pre.next;
        pre.next = newNode;
        pre = newNode.next;
    }
 
    let pre2 = pHead;
    let head = pHead.next;
    while(pre2.next.next!==null){
        let temp = pre2.next;
        if(pre2.random!=null){
            temp.random = pre2.random.next;
        }
        pre2 = temp.next;
        temp.next = pre2.next;
    }
    return head;
}

let head = Clone(a)

</script>